/**
 * JAVACC DEMO 1.0
 * @copy right iussoft company All rights reserved.
 * @Package com.iussoft.portal.tags
 */
package com.apache.portal.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * description:  自定义分页标签
 * @author Hou Dayu 创建时间：2016-8-24
 */
@SuppressWarnings("serial")
public class PortalPagerTag extends TagSupport {

	private int curpage;// 当前页
	private int pagesize; // 页大小
	private int totalLines; // 总记录条数
	private int totalPages;

	//private JspWriter out;

	public int doStartTag() throws JspException {
		boolean b = false;
		try {
			JspWriter out = this.pageContext.getOut();
			StringBuffer sb = new StringBuffer("");
			int total_page = totalPages();
			//计算开始页
			int _start = curpage - 2;
			_start = _start < 1 ? 1 : _start;
			//计算结束页
			int _end = curpage + 2;
			_end = _end > total_page ? total_page : _end;
			int _curPageNum = _end - _start + 1;
			//左调整
			if (_curPageNum < 10 && _start > 1) {
				_start = _start - (10 - _curPageNum);
				_start = _start < 1 ? 1 : _start;
				_curPageNum = _end - _start + 1;
			}
			//右边调整
			if (_curPageNum < 10 && _end < total_page) {
				_end = _end + (10 - _curPageNum);
				_end = _end > total_page ? total_page : _end;
			}
			String strs = "";
			String strsBefore = "";
			String strsAfter = "";
			if (_start > 1) {
				int _upPage = curpage - 1;
				if (_upPage == 0)
					_upPage = 1;
				strsBefore = "<a class='sidePage' href='javascript:void(0);' onclick=\"gotoPage(" + _upPage
						+ ")\"><<上一页</a>";
			}
			if (_end < total_page) {
				int _nextPage = curpage + 1;
				if (_nextPage > totalLines)
					_nextPage = totalLines;
				strsAfter = "<a class='sidePage' href='javascript:void(0);' onclick=\"gotoPage(" + _nextPage
						+ ")\">下一页>></a>";
			}
			for (int i = _start; i <= _end; i++) {
				if (i == curpage) {
					strs += "<a class='current' href='javascript:void(0);'>" + i + "</a>";
				} else {
					if (i > 0) {
						strs += "<a href='javascript:void(0);' onclick=\"gotoPage(" + i + ")\">" + i + "</a>";
					}
				}
			}
			strs = strsBefore + strs + strsAfter;
			sb.append(strs);
			out.println(sb.toString());
			out.flush();
		} catch (Exception e) {
		}
		if (b) {
			return super.EVAL_BODY_INCLUDE;
		} else {
			return super.SKIP_BODY;
		}
	}

	//	public int doEndTag() {
	//		try {
	//			if (null != out)
	//				out.close();
	//		} catch (Exception e) {
	//		}
	//		return super.EVAL_BODY_INCLUDE;
	//	}

	public int getCurpage() {
		return curpage;
	}

	public void setCurpage(int curpage) {
		this.curpage = curpage;
	}

	public int getPagesize() {
		return pagesize;
	}

	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}

	public int getTotalLines() {
		return totalLines;
	}

	public void setTotalLines(int totalLines) {
		this.totalLines = totalLines;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	/**
	 * 计算总页数
	 * @return
	 */
	private int totalPages() {
		if (totalPages > 0)
			return totalPages;
		return totalLines % pagesize == 0 ? totalLines / pagesize : totalLines / pagesize + 1;
	}

}
